﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Microsoft.SqlServer.Management.Smo;
using System.Data.SqlClient;
using System.Collections;
using System.Collections.Specialized;
using System.IO;

namespace Sys.SqlServer.Smo.Gui
{
    public partial class DataAnalyzer : Form
    {
        public DataAnalyzer()
        {
            InitializeComponent();

            Builder.DatabaseChanged += new EventHandler(OnDatabaseChanged);

            txtDataFile.Text = Path.Combine(Environment.GetFolderPath(Environment.SpecialFolder.DesktopDirectory), "data.xml");
        }

        private void OnDatabaseChanged(object sender, EventArgs e)
        {
            LoadTables(Builder.ConnectionString.Database);
        }

        private void OnClose(object sender, EventArgs e)
        {
            Close();
        }

        private void LoadTables(string database)
        {
            new Action(delegate()
            {
                using (SqlConnection c = new SqlConnection(Builder.ConnectionString.ToString()))
                {
                    Server s = new Server(new Microsoft.SqlServer.Management.Common.ServerConnection(c));

                    Microsoft.SqlServer.Management.Smo.Database db = s.Databases[database];

                    lstSelectedTables.Items.Clear();

                    lstAvailableTables.Items.Clear();
                    foreach (Microsoft.SqlServer.Management.Smo.Table t in db.Tables)
                        lstAvailableTables.Items.Add(t.Name);
                }
            }).BeginInvoke(delegate(IAsyncResult ar) { }, null);
        }

        private void OnAddRemoveItems(object sender, EventArgs e)
        {
            ListBox from = null;
            ListBox to = null;

            #region Switch

            switch (((Button)sender).Name)
            {
                case "btnAddTables":
                    from = lstAvailableTables;
                    to = lstSelectedTables;
                    break;

                case "btnRemoveTables":
                    from = lstSelectedTables;
                    to = lstAvailableTables;
                    break;
            }

            #endregion

            List<object> items = new List<object>();

            foreach (object o in from.SelectedItems)
            {
                items.Add(o);
                to.Items.Add(o);
            }

            foreach (object o in items)
                from.Items.Remove(o);
        }

        private void OnGenerateData(object sender, EventArgs e)
        {
            if (string.IsNullOrEmpty(txtDataFile.Text))
                OnDataFileBrowsed(sender, e);
        }

        private void OnDataFileBrowsed(object sender, EventArgs e)
        {
            if (rbtnSerialize.Checked)
            {
                SaveFileDialog d = new SaveFileDialog();
                d.Filter = "XML Files|*.xml";
                d.AddExtension = true;
                d.CheckFileExists = false;
                d.CheckPathExists = false;

                if (d.ShowDialog() == DialogResult.OK)
                    txtDataFile.Text = d.FileName;
            }

            if (rbtnDeserialize.Checked)
            {
                OpenFileDialog d = new OpenFileDialog();
                d.Filter = "XML Files|*.xml";
                d.AddExtension = true;
                d.CheckFileExists = true;
                d.CheckPathExists = true;
                d.Multiselect = false;

                if (d.ShowDialog() == DialogResult.OK)
                    txtDataFile.Text = d.FileName;
            }
        }
    }
}